// pages/batchAddRoomsConfirm/batchAddRoomsConfirm.js
var that = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    configIndex: -1,
    confingRoomsIndex:0,
    houseData: {}
  },

//整个楼层添加
  floorChack: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    that.data.houseData.floors[index].check = !that.data.houseData.floors[index].check;
    for (var i in that.data.houseData.floors[index].rooms) {
      if (that.data.houseData.floors[index].rooms[i].config == -1 || that.data.houseData.floors[index].rooms[i].config == this.data.configIndex){
        that.data.houseData.floors[index].rooms[i].check = that.data.houseData.floors[index].check;
        //判断为应用或者取消
        if (that.data.houseData.floors[index].rooms[i].check) {
          that.data.houseData.floors[index].rooms[i].config = this.data.configIndex;
        } else {
          that.data.houseData.floors[index].rooms[i].config = -1;
        }
      }
      
    }
    this._setData();
  },
  
  // 点击每个房间
  roomsCkeck: function (e) {

    var index = e.currentTarget.dataset.index;
    var idx = e.currentTarget.dataset.idx;
    console.log(index)
    //判断为其他配置已经应用的房号
    if (that.data.houseData.floors[index].rooms[idx].config == -1 || that.data.houseData.floors[index].rooms[idx].config == this.data.configIndex) {
      that.data.houseData.floors[index].rooms[idx].check = !that.data.houseData.floors[index].rooms[idx].check;
      //判断为应用或者取消
      if (that.data.houseData.floors[index].rooms[idx].check) {
        that.data.houseData.floors[index].rooms[idx].config = this.data.configIndex;
      } else {
        that.data.houseData.floors[index].rooms[idx].config = -1;
      }
      for (var i in that.data.houseData.floors[index].rooms) {
        //如果已经是选择就并有一个取消就取消全选
        if (that.data.houseData.floors[index].check && !that.data.houseData.floors[index].rooms[idx].check) {
          that.data.houseData.floors[index].check = false;
        }
        //判断有一个不一样就跳出循环
        if (that.data.houseData.floors[index].rooms[i].check != that.data.houseData.floors[index].rooms[idx].check) {
          break;
        }//全部一样就为全选
        if (i == that.data.houseData.floors[index].rooms.length - 1) {
          that.data.houseData.floors[index].check = that.data.houseData.floors[index].rooms[idx].check
        }

      }
      this._setData();
    }

  },
  //确定
  submit: function () {
    //保存数据
    wx.setStorageSync('rooms', this.data.houseData);
    // 统计应用的房号数量
    that.countRoom();
    wx.navigateBack({
    })
  },
  //获取应用的房号数量
  countRoom:function(){
     var house = that.data.houseData
     for (var i in house.floors){
       for (var j in house.floors[i].rooms) { 
         if (house.floors[i].rooms[j].config == this.data.configIndex){
           this.data.confingRoomsIndex++;
         }
       }
     }
     //通知用户点击确定
     wx.setStorageSync('configOnRooms', this.data.confingRoomsIndex);
  },
  //更新data
  _setData: function () {
    that.setData({
      houseData: that.data.houseData
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.index + 'ssssssssssssssssss')
    if (options.index) {
      this.setData({
        configIndex: options.index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    that = this;
    var house = wx.getStorageSync('rooms');
    // wx.removeStorageSync('rooms')
    
    console.log(house);
    if (house) {
      this.setData({
        houseData: house,
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})